package com.gdut.huayan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gdut.huayan.constant.SecurityConstant;
import com.gdut.huayan.dao.LocationMapper;
import com.gdut.huayan.dao.StockMapper;
import com.gdut.huayan.entity.dto.PageQuery;
import com.gdut.huayan.entity.po.Location;
import com.gdut.huayan.entity.po.Stock;
import com.gdut.huayan.service.FileService;
import com.gdut.huayan.service.LocationService;
import com.gdut.huayan.service.exception.BaseException;
import com.gdut.huayan.utils.AliyunUtils;
import com.gdut.huayan.utils.SecurityUtils;
import com.gdut.huayan.utils.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.*;

@Service
public class LocationServiceImpl extends ServiceImpl<LocationMapper, Location> implements LocationService {

    @Autowired
    private StockMapper stockMapper;

    @Autowired
    private FileService fileService;

    @Override
    public PageInfo<Location> getPage(PageQuery pageQuery) {
        PageHelper.startPage(pageQuery.getPage(), pageQuery.getSize());
        QueryWrapper<Location> wrapper = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(pageQuery.getSearch())){
            wrapper.like("title", "%" + pageQuery.getSearch() + "%");
        }
        if("ASC".equals(pageQuery.getOrder())){
            wrapper.orderByAsc(pageQuery.getOrderKey().split(";"));
        }
        if("DESC".equals(pageQuery.getOrder())){
            wrapper.orderByDesc(pageQuery.getOrderKey().split(";"));
        }

        /*locations.getList().forEach(l -> {
            l.setEffectiveImage(AliyunUtils.getDownloadLink(l.getEffectiveImage(),new Date().getTime() + SecurityConstant.TIME_TEN_MINUTES));
            l.setImageFileId(AliyunUtils.getDownloadLink(l.getImagePath(),new Date().getTime() + SecurityConstant.TIME_TEN_MINUTES));
        });*/
        return new PageInfo<>(baseMapper.selectList(wrapper));
    }

    @Override
    public List<Location> get(Long festivalId) {
        QueryWrapper<Stock> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("location_id");
        queryWrapper.eq("festival_id",festivalId);
        List<Stock> stocks = stockMapper.selectList(queryWrapper);
        if(stocks.size() == 0){
            return null;
        }
        List<Long> selectIds = new ArrayList<>();
        stocks.forEach( s -> selectIds.add(s.getLocationId()));
        return baseMapper.selectBatchIds(selectIds);
    }

    @Override
    public boolean save(Location location) {
        location.setCreateUser(SecurityUtils.getUserIdNumber());
        return super.save(location);
    }

    @Override
    public Map<String, String> getUploadLink() {
        Map<String, String> result = new HashMap<>();
        long now = new Date().getTime();
        String filePath = "location/" + UUID.randomUUID() + now + ".jpeg";
        String uploadLink = AliyunUtils.getUploadLink(filePath,"image/jpeg", now + SecurityConstant.TIME_TEN_MINUTES);
        result.put("filePath", filePath);
        result.put("uploadLink", uploadLink);
        return result;
    }

    @Override
    public boolean removeById(Serializable id){
        isCanOperation(id);
        Location deleteLocation = baseMapper.selectById(id);
        if (deleteLocation == null){
            return true;
        }
        /*AliyunUtils.deleteFile(deleteLocation.getImagePath());
        AliyunUtils.deleteFile(deleteLocation.getEffectiveImage());*/
        return super.removeById(id);
    }

    @Override
    public boolean updateById(Location location){
        location.setCreateUser(null);
        location.setEffectiveImageFileId(null);
        location.setImageFileId(null);
        return super.updateById(location);
    }

    private void  isCanOperation(Serializable id){
        QueryWrapper<Stock> wrapper = new QueryWrapper<>();
        wrapper.eq("location_id",id);
        int count = stockMapper.selectCount(wrapper);
        if(count > 0){
            throw new BaseException(4000,"不允许操作正在使用中的法堂");
        }
    }
}
